﻿using Avalonia.Controls;
using Avalonia.VisualTree;
using MyTodolist.ViewModels;
using System.Linq;

namespace MyTodolist.Views;

public partial class MainView : UserControl
{
    public MainView()
    {
        InitializeComponent();
    }

    private void TextBox_KeyDown(object? sender, Avalonia.Input.KeyEventArgs e)
    {
        var dataContext = GetDataContext();
        if (dataContext is null) return;

        var textBox = sender as TextBox;
        if (textBox is null) return;

        var itemViewModel = textBox.DataContext as TodoListItem;

        if (e.Key == Avalonia.Input.Key.Enter)
        {
            dataContext.AddItemCommand?.Execute(null);

            var lastItem = PART_ItemsControl.ItemsView.LastOrDefault();
            var itemContentPresenter = PART_ItemsControl.ContainerFromItem(lastItem);
            if (itemContentPresenter is null) return;
            itemContentPresenter.Loaded += ItemContentPresenter_Loaded;
           
        }
        else if (e.Key == Avalonia.Input.Key.Up)
        {
            var index = itemViewModel.Index - 1; // 因为 Index 开始是 1
            var nextIndex = index - 1;
            if (nextIndex < 0) return;
            var itemContentPresenter = PART_ItemsControl.ContainerFromIndex(nextIndex);
            if (itemContentPresenter is null) return;
            FocusItemContentPresenter(itemContentPresenter);
        }
        else if (e.Key == Avalonia.Input.Key.Down)
        {
            var index = itemViewModel.Index - 1; // 因为 Index 开始是 1
            var nextIndex = index + 1;
            if (nextIndex >= dataContext.TodoList.Count) return;
            var itemContentPresenter = PART_ItemsControl.ContainerFromIndex(nextIndex);
            if (itemContentPresenter is null) return;
            FocusItemContentPresenter(itemContentPresenter);
        }
    }

    private void ItemContentPresenter_Loaded(object? sender, Avalonia.Interactivity.RoutedEventArgs e)
    {
        var control = sender as Control;
        if (control is null) return;
        control.Loaded -= ItemContentPresenter_Loaded;
        FocusItemContentPresenter(control);
    }

    private void Button_Click(object? sender, Avalonia.Interactivity.RoutedEventArgs e)
    {
        var dataContext = GetDataContext();
        if (dataContext is null) return;

        dataContext.AddItemCommand?.Execute(null);

        var lastItem = PART_ItemsControl.ItemsView.LastOrDefault();
        var itemContentPresenter = PART_ItemsControl.ContainerFromItem(lastItem);
        if (itemContentPresenter is null) return;
        itemContentPresenter.Loaded += ItemContentPresenter_Loaded;
    }

    private MainViewModel GetDataContext()
    {
        return DataContext as MainViewModel;
    }

    private void FocusItemContentPresenter(Control itemContentPresenter)
    {
        if (itemContentPresenter is null) return;
        var grid = itemContentPresenter.GetVisualChildren().OfType<Grid>().FirstOrDefault();
        if (grid is null) return;
        var textBox = grid.Children.OfType<TextBox>().FirstOrDefault();
        if (textBox is null) return;
        textBox.Focus();
    }



}
